package pers.cz.analysis.handle;

import pers.cz.analysis.entitys.ClassDesc;
import pers.cz.analysis.enums.AnalysisConf;
import pers.cz.grammar.antlr.java.Java8BaseVisitor;
import pers.cz.grammar.antlr.java.Java8Parser;
import pers.cz.jefconfig.config.JefConfiguration;
import pers.cz.jefconfig.tools.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: Jef-code-analysis
 * @description: 类观察器
 * @author: Cheng Zhi
 * @create: 2023-10-11 17:08
 **/
public class ClassVisitor extends Java8BaseVisitor<Integer> {

    private String className = null;
    private String packageName = null;
    private String methodName = null;

    private List<ClassDesc> classDescs = new ArrayList<ClassDesc>();

    public List<ClassDesc> getClassDescs() {
        return classDescs;
    }

    /**
     * 类名信息
     * @param ctx
     * @return
     */
    @Override
    public Integer visitClassName(Java8Parser.ClassNameContext ctx) {
        className = ctx.getText();
        return null;
    }

    /**
     * 包名
     * @param ctx
     * @return
     */
    @Override
    public Integer visitPackageName(Java8Parser.PackageNameContext ctx) {
        packageName = ctx.getText();
        return null;
    }

    /**
     * 方法信息
     * @param ctx
     * @return
     */
    @Override
    public Integer visitMethodDeclaration(Java8Parser.MethodDeclarationContext ctx) {
        methodName = ctx.methodHeader().getChild(1).getText();
        String methodBody = ctx.methodBody().getText();

        String keyWords = JefConfiguration.get(AnalysisConf.JEF_ANALYSIS_KEYWORDS);
        String[] split = StringUtils.split(keyWords, ";");

        for (String keyWord : split) {
            if (JefConfiguration.getBoolean(AnalysisConf.JEF_ANALYSIS_IGNORECASE, true)) {
                if (methodBody.toLowerCase().contains(keyWord)) {

                    ClassDesc classDesc = new ClassDesc();
                    classDesc.setFullClassName(packageName + "." + className);
                    classDesc.setMethodName(methodName);
                    classDesc.setKeyWord(keyWord);
                    classDescs.add(classDesc);
                }
            }
        }
        return null;
    }

}
